package com.hm.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class FileUtil {
	
	public static void copyFile(File tarFile, InputStream srcIn) throws Exception{
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(tarFile);
			byte[] buffer = new byte[512];
			int length = -1;
			while(-1 != (length = srcIn.read(buffer))){
				fos.write(buffer, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("拷贝文件异常！");
		} finally {
			if (fos != null){
				fos.close();
			}
		}
	}
	
	public static void copyFile(String tarPath, String fileName, File srcFile) throws Exception{
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			File tarFile = new File(tarPath + File.separatorChar + fileName);
			fis = new FileInputStream(srcFile);
			fos = new FileOutputStream(tarFile);
			byte[] buffer = new byte[512];
			int length = -1;
			while(-1 != (length = fis.read(buffer))){
				fos.write(buffer, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("拷贝文件异常！");
		} finally {
			if (fis != null){
				fis.close();
			}
			if (fos != null){
				fos.close();
			}
		}
	}
}

